package com.kehao.leetcode.hard.array;

import org.junit.Test;

public class MaxAreaSolution {
    //暴力解法 会超时
    public int maxArea1(int[] height) {
        int result =0;
        int n = height.length;
        for (int i = 0; i < n; i++) {
            for (int j =i+1; j < n; j++) {
                int area = Math.min(height[i],height[j]) * (j-i);
                result = result>area?result:area;
            }
        }
        return  result;
    }


    //双指针法
    public int maxArea(int[] height) {
        int result =0;
        int left = 0 , right = height.length-1;
        while (left<right){
            int area = Math.min(height[left],height[right])*(right-left);
            result = result>area?result:area;
            if(height[left]<height[right]){
                left++;
            }else {
                right--;
            }
        }
        return  result;
    }


    @Test
    public void test01(){
        int[] height = {1,8,6,2,5,4,8,3,7};
        int result = maxArea(height);//49
        System.out.println(result);
    }

    @Test
    public void test02(){
        int[] height = {1,1};
        int result = maxArea(height);//1
        System.out.println(result);
    }

    @Test
    public void test03(){
        int[] height = {4,3,2,1,4};
        int result = maxArea(height);//16
        System.out.println(result);
    }

    @Test
    public void test04(){
        int[] height = {1,2,1};
        int result = maxArea(height);//2
        System.out.println(result);
    }
}
